package com.dw.wms.basedata.controller;
import com.dw.wms.basedata.domain.Goods;
import com.dw.wms.basedata.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basedata/goods")
@RequiredArgsConstructor
@Api(tags = "货品管理")
public class GoodsController {
    private final GoodsService goodsService;

    /**
     * 获取所有货品列表
     */
    @GetMapping
    @ApiOperation("获取货品列表")
    public ResponseEntity<List<Goods>> getAllGoods() {
        List<Goods> goodsList = goodsService.list();
        return ResponseEntity.ok(goodsList);
    }

    /**
     * 根据ID获取货品详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取货品详情")
    public ResponseEntity<Goods> getGoodsById(@PathVariable Long id) {
        Goods goods = goodsService.getById(id);
        if (goods != null) {
            return ResponseEntity.ok(goods);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 创建新货品
     */
    @PostMapping
    @ApiOperation("创建货品")
    public ResponseEntity<Goods> createGoods(@RequestBody Goods goods) {
        // 检查必要字段
        if (goods.getGoodsCode() == null || goods.getGoodsName() == null) {
            return ResponseEntity.badRequest().build();
        }

        // 设置默认状态
        if (goods.getStatus() == null) {
            goods.setStatus("0");
        }

        boolean saved = goodsService.save(goods);
        if (saved) {
            Goods savedGoods = goodsService.getById(goods.getId());
            return ResponseEntity.status(HttpStatus.CREATED).body(savedGoods);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 更新货品信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新货品信息")
    public ResponseEntity<Goods> updateGoods(@PathVariable Long id, @RequestBody Goods goods) {
        // 检查要更新的货品是否存在
        Goods existingGoods = goodsService.getById(id);
        if (existingGoods == null) {
            return ResponseEntity.notFound().build();
        }

        // 设置ID确保更新正确的记录
        goods.setId(id);

        // 执行更新操作
        boolean updated = goodsService.updateById(goods);
        if (updated) {
            // 返回更新后的完整对象
            Goods updatedGoods = goodsService.getById(id);
            return ResponseEntity.ok(updatedGoods);
        } else {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 删除货品
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除货品")
    public ResponseEntity<Void> deleteGoods(@PathVariable Long id) {
        boolean deleted = goodsService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
